package xm
{
	import avmplus.getQualifiedClassName;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	
	import xm.components.checkbox.CheckBox;
	import xm.debug.log;

	/**
	 * 런타임이 아닌 플래시 IDE 에서 적용되는 클래스입니다.<br />
	 * <br />
	 * @author winnie
	 */	
	public class LivePreviewTester
	{
		
		/*
		DEBUG_LivePreview.setStageAndSize( this.parent, 0, 300 );
		log( this.parent + " : "+ this.parent.height ); // [object LivePreviewParent]	13
		log( this.parent.parent + " : "+ this.parent.parent.height ); // [ object StageDisplayLeafSelectable] 469
		log( this.parent.parent.parent + " : "+ this.parent.parent.parent.height );// [object Sprite] 469
		log( this.parent.parent.parent.parent + this.parent.parent.parent.parent.height );//[object StageDisplayShapeSelectable] 469
		*/
		
		/**
		 * 런타임인지 아닌지 체크함.<br /> 
		 * @param obj 화면에 있는 아무 displayObject를 넣음.
		 * @return 
		 * 
		 */		
		static public function isLive( obj : DisplayObject ):Boolean
		{
			return (obj.parent != null && getQualifiedClassName(obj.parent) == "fl.livepreview::LivePreviewParent");
		}
		
		
		/**
		 * @param container 로부터 부모리스트 logging
		 */		
		static public function showParent( container : DisplayObjectContainer ):void
		{
			log( container.toString() );
			
			if( container.parent != null ) showParent( container.parent );
		}
		
		
		/**
		 * @param container 로부터 자식리스트 logging.
		 * @param depth - 로 뎁스표시.
		 */		
		static public function showChildren( container : DisplayObjectContainer, depth : String = "" ):void
		{
			var i:int, len:int;
			i=0;
			len = container.numChildren;
			
			depth += "-";
			
			while( i<len )
			{
				log( depth+" " + i.toString()+ " " + container.getChildAt( i ) + "  " + 
					( container.getChildAt( i ) as DisplayObjectContainer ? ( container.getChildAt( i ) as DisplayObjectContainer ).numChildren : "" ) + 
					"   " + container.getChildAt( i ).width, container.getChildAt( i ).height, 
					container.getChildAt( i ).x, container.getChildAt( i ).y );
				
				if( container.getChildAt( i ) is DisplayObjectContainer )
				{
					showChildren( container.getChildAt( i ) as DisplayObjectContainer, depth );
				}
				++i;
			}
		}
		
		/**
		 * prev 상태에서 stage에 있는 displayObject 추출. Array 로 반환한다.
		 * <br />
		 * <br />
		 * @param obj
		 * @return 
		 */		
		static public function getDisObjListFromStage( obj : DisplayObject ):Array
		{
			var stageDisplayLeafSelectableList : Array,
				stageDisplayShapeSelectable : Object,
				childFromShapeSelectable : Object,
				deepChild : Object,
				i:int, len:int;
				
			stageDisplayShapeSelectable = getStageDisplayShapeSelectable( obj );
			
			stageDisplayLeafSelectableList = [];
			
			i=0;
			len = stageDisplayShapeSelectable.numChildren as int;
			while( i<len )
			{
				childFromShapeSelectable = stageDisplayShapeSelectable.getChildAt( i );
				
				if( childFromShapeSelectable.toString() == "[object StageDisplayLeafSelectable]" )
				{
					deepChild = childFromShapeSelectable.getChildAt( 0 );
					if( deepChild.toString() == "[object Sprite]" )
					{
						stageDisplayLeafSelectableList.push( deepChild );
					}
					else if( deepChild.toString() == "[object LivePreviewParent]" )
					{
						stageDisplayLeafSelectableList.push( deepChild.getChildAt( 0 ) );
					}
				}
				++i;
			}
			
			return stageDisplayLeafSelectableList;
		}
		
		
		
		
		
		static private function getStageDisplayShapeSelectable( obj : Object ):Object
		{
			if( obj && obj.parent && obj.parent.toString() == "[object StageDisplayShapeSelectable]" ) return obj;
			else
			{
				return getStageDisplayShapeSelectable( obj.parent );
			}
		}
	}
}